package top.longmarch.flow.utils;

import lombok.Data;

import java.io.Serializable;

/**
 * 响应封装
 *
 * @author yuyue
 */
@Data
public class Result<T> implements Serializable {

    /**
     * 成功响应编码
     */
    public static final int RESPOND_SUCCEED_CODE = 1000;
    /**
     * 成功响应描述
     */
    public static final String RESPOND_SUCCEED_MESSAGE = "succeed";
    /**
     * 失败响应编码
     */
    public static final int RESPOND_FAIL_CODE = 5000;
    /**
     * 失败相应描述
     */
    public static final String RESPOND_FAIL_MESSAGE = "fail";

    private int code;
    private String message;
    private T data;

    private Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> Result<T> ok(int code, String message, T t) {
        return new Result<>(code, message, t);
    }

    public static <T> Result<T> ok(T t) {
        return ok(RESPOND_SUCCEED_CODE, RESPOND_SUCCEED_MESSAGE, t);
    }

    public static <T> Result<T> ok() {
        return ok(null);
    }

    public static <T> Result<T> fail(int code, String message, T t) {
        return new Result<>(code, message, t);
    }

    public static <T> Result<T> fail(int code, String message) {
        return new Result<>(code, message, null);
    }

    public static <T> Result<T> fail(String message) {
        return fail(RESPOND_FAIL_CODE, message);
    }

    public static <T> Result<T> fail() {
        return fail(RESPOND_FAIL_MESSAGE);
    }

}
